﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

namespace Framework.ProcessBench
{
    class Program
    {
        static void Main(string[] args)
        {
            if (args.Any() && args[0] == "Timeout")
            {
                Thread.Sleep(1000);
            }
            
            if (args.Any() && args[0] == "ExitCode10")
            {
                Environment.Exit(10);
            }
            
            if (args.Any() && args[0] == "ExitCodeTest")
            {
                Environment.Exit(-1);
            }

            if (args.Any() && args[0] == "ErrorOutput")
            {
                Console.Error.WriteLine("error");
                Environment.Exit(0);
            }

            var stopWatch = new Stopwatch();

            stopWatch.Start();

            const int count = 20000;
            Action p1 = () => Print(count, Console.Out, "std");
            Action p2 = () => Print(count, Console.Error, "err");

            var actions = new List<Action> { p1, p2 };

            Parallel.ForEach(actions, a => a());

            stopWatch.Stop();

            Console.WriteLine(stopWatch.Elapsed);

            Environment.Exit(0);
        }

        private static void Print(int count, TextWriter writer, string message)
        {
            for (var i = 0; i < count; i++)
            {
                writer.WriteLine(message);
            }
        }
    }
}
